import { Router, Redirect, Route, Switch } from 'react-router-dom'
import history from './utils/history'

// import Layout from '@/pages/Layout'
// import Login from '@/pages/Login'
// import NotFound from '@/pages/NotFound'
import AuthRoute from '@/components/AuthRoute'

import { lazy, Suspense } from 'react'
// 导入页面组件
const Login = lazy(() => import('./pages/Login'))
const Layout = lazy(() => import('./pages/Layout'))
const NotFound = lazy(() => import('./pages/NotFound'))

const App = () => {
  return (
    <Router history={history}>
      {/* 注意一个坑 全屏 */}
      <Suspense
        fallback={
          <div
            style={{
              textAlign: 'center',
              marginTop: 200,
            }}
          >
            loading...
          </div>
        }
      >
        <Switch>
          <Redirect exact from="/" to="/home"></Redirect>
          {/* <Route path="/home" component={Layout}></Route> */}
          {/* <Route
          path="/home"
          render={() => {
            const token = getToken()
            if (token) {
              return <Layout></Layout>
            } else {
              return <Redirect to="/login"></Redirect>
            }
          }}
        ></Route> */}

          <AuthRoute path="/home" component={Layout}></AuthRoute>
          <Route path="/login" component={Login}></Route>
          <Route component={NotFound}></Route>
        </Switch>
      </Suspense>
    </Router>
  )
}

export default App
